package com.wxapp.system.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.system.api.pojo.entity.DictKeyDO;
import com.wxapp.system.api.pojo.entity.DictValueDO;
import com.wxapp.system.api.pojo.vm.dict.key.PDictKeyDetailVm;
import com.wxapp.system.api.pojo.vm.dict.key.PDictKeyPageVm;
import com.wxapp.system.api.pojo.vm.dict.key.QDictKeySelectVm;
import com.wxapp.system.api.pojo.vo.dict.key.QDictKeyPageVo;
import com.wxapp.system.api.pojo.vo.dict.key.QDictKeySaveVo;
import com.wxapp.system.api.pojo.vo.dict.key.QDictKeyUpdateVo;
import com.wxapp.system.service.mapper.DictKeyMapper;
import com.wxapp.system.service.mapper.DictValueMapper;
import com.wxapp.system.service.service.DictKeyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lgl
 * @Description 字典值
 * @Datetime 2022/12/8 17:58
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class DictKeyServiceImpl extends IBaseService implements DictKeyService {
    private final DictValueMapper dictValueMapper;
    private final DictKeyMapper dictKeyMapper;

    @Override
    public ResultVm saveDictKey(UserVo userVo, QDictKeySaveVo qDictKeySaveVo) {
        LambdaQueryWrapper<DictKeyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictKeyDO::getStatus, true).eq(DictKeyDO::getDictKey, qDictKeySaveVo.getDictKey());
        var selectCount = dictKeyMapper.selectCount(wrapper);
        if (selectCount > 0) {
            return error("已存在该名称的字典");
        }
        var dictKeyDO = CopyUtil.copyObjectNew(qDictKeySaveVo, DictKeyDO::new, (s, r) -> {
            r.setCreateBy(userVo.getId());
            return null;
        });
        dictKeyMapper.insert(dictKeyDO);
        return success("保存成功");
    }

    @Override
    public ResultVm<String> updateDictKey(UserVo userVo, QDictKeyUpdateVo qSaveVo) {
        var dictKeyDO = dictKeyMapper.selectById(qSaveVo.getId());
        if (CheckUtil.isNullObject(dictKeyDO)) {
            return error("查询到该字典信息");
        }
        var dictKey = CopyUtil.copyObjectNew(qSaveVo, DictKeyDO::new, (s, r) -> {
            r.setUpdateBy(userVo.getId());
            return null;
        });
        dictKeyMapper.updateById(dictKey);
        return success("更新成功");
    }

    @Override
    public ResultVm<PDictKeyDetailVm> detailDictKey(String id) {
        var dictKeyDO = dictKeyMapper.selectById(id);
        if (CheckUtil.isNullObject(dictKeyDO)) {
            return error("没有找到该字典值");
        }
        return success(CopyUtil.copyObjectNew(dictKeyDO, PDictKeyDetailVm::new, null));

    }

    @Override
    public ResultVm<Page<PDictKeyPageVm>> pageDictKey(QDictKeyPageVo qSaveVo) {
        Map<SFunction<DictKeyDO, ?>, SqlKeyword> conditions = new HashMap<>();
        conditions.put(DictKeyDO::getDictName, SqlKeyword.LIKE);
        var condition = ConditionUtil.getSCondition(DictKeyDO::new, qSaveVo, null, conditions,null);
        var dictKeyDOPage = dictKeyMapper.selectPage(ConditionUtil.getPage(qSaveVo, DictKeyDO.class), condition);
        var pPageVmPage = CopyUtil.copyPage(dictKeyDOPage, PDictKeyPageVm::new, null);
        return success(pPageVmPage);
    }

    @Override
    public ResultVm<String> deleteDictKey(String id) {
        var dictKeyDO = dictKeyMapper.selectById(id);
        if (CheckUtil.isNullObject(dictKeyDO)) {
            return error("没有找到该字典值");
        }
        LambdaQueryWrapper<DictValueDO> query = new LambdaQueryWrapper<>();
        query.eq(DictValueDO::getKeyId, id).eq(DictValueDO::getStatus, true);
        if (dictValueMapper.isExist(query)) {
            return error("该字典下存在值请先删除该部分");
        }
        LambdaUpdateWrapper<DictKeyDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DictKeyDO::getStatus, true).eq(DictKeyDO::getId, id).set(DictKeyDO::getStatus, false);
        dictKeyMapper.update(null, wrapper);
        return success();
    }

    @Override
    public ResultVm<List<QDictKeySelectVm>> getSelect(String pid) {
        LambdaQueryWrapper<DictKeyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictKeyDO::getStatus, true).select(DictKeyDO::getId, DictKeyDO::getDictName).eq(CheckUtil.isNotNullObject(pid), DictKeyDO::getPid, pid);
        var dictKeyDOS = dictKeyMapper.selectList(wrapper);
        return success(CopyUtil.copyListNew(dictKeyDOS, QDictKeySelectVm::new, (s, r) -> {
            r.setName(s.getDictName());
            return null;
        }));
    }
}
